﻿using LYL.Common.RT.Text;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeixinSb.Model;
using Windows.Storage.Streams;
using Windows.Web.Http;

namespace WeixinSB.Service
{
    public class UploadService
    {
        const string TYPE = "ZhuangBi";

        const string HOST_URL = "http://wpapi.wp1314.com";

        //const string HOST_URL = "http://192.168.1.4:807";
        const string Upload_URI = HOST_URL + "/WeixinSB/Upload";
        const string LIST_URI = HOST_URL + "/WeixinSB/List";
        const string LIKE_URI = HOST_URL + "/WeixinSB/Like";


        /// <summary>
        /// 点赞Y，鄙视N
        /// </summary>
        /// <param name="maxId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public async Task<BaseUploadModel> Like(string id, string status)
        {
            List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
            paramList.Add(new KeyValuePair<string, string>("id", id));
            paramList.Add(new KeyValuePair<string, string>("like", status));

            HttpFormUrlEncodedContent form = new HttpFormUrlEncodedContent(paramList);

            return await Request(form, LIST_URI);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="maxId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public async Task<BaseUploadModel> List(string maxId, int count = 20)
        {
            List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
            paramList.Add(new KeyValuePair<string, string>("max_id", maxId));
            paramList.Add(new KeyValuePair<string, string>("type", TYPE));
            paramList.Add(new KeyValuePair<string, string>("count", count.ToString()));
            HttpFormUrlEncodedContent form = new HttpFormUrlEncodedContent(paramList);

            return await Request(form, LIST_URI);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<BaseUploadModel> UploadFile(string title,string type,string models,string deviceName, IInputStream stream)
        {
            var inputStream = new HttpStreamContent(stream);
           
            HttpMultipartFormDataContent mul = new HttpMultipartFormDataContent("----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
            mul.Add(inputStream, "pic", "test.jpg");
            mul.Add(new HttpStringContent(title), "title");
            mul.Add(new HttpStringContent(type), "type");
            mul.Add(new HttpStringContent(models), "models");
            mul.Add(new HttpStringContent(deviceName), "deviceName");

            return await Request(mul, Upload_URI);
        }


        /// <summary>
        /// 基本请求
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<BaseUploadModel> Request(IHttpContent iHttpContent, string uri)
        {
            BaseUploadModel model = new BaseUploadModel();

            HttpClient http = new HttpClient();

            var res = await http.PostAsync(new Uri(uri, UriKind.Absolute), iHttpContent);
            if (res.StatusCode == HttpStatusCode.Ok)
            {
                var json = await res.Content.ReadAsStringAsync();
                model = JsonHelper.JsonToObj<BaseUploadModel>(json,false);
            }

            return model;
        }
    }

    public class BaseUploadModel : BaseUploadResponseModel<List<WeiXinUploadModel>>
    {

    }


    public class BaseUploadResponseModel<T>
    {/// <summary>
        /// 返回值
        /// </summary>
        public bool Ret { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 返回数据
        /// </summary>
        public T DataModel { get; set; }
    }


}
